#include "Vofa_visualization.h"

SCB_visual_flag_t SCB_Visual_Flag_t;

void init_out()
{
	//initial Lower computer
		__HAL_UART_ENABLE_IT(&USRAT_PORT,UART_IT_IDLE);
		HAL_UART_Receive_DMA(&USRAT_PORT,(uint8_t *)buffer,100);
    printf("\r\nwelcome to use SCB-contorl\r\n");
    printf("\r\ninput \'help\' to show all command \r\n");
}

/**
 * @description: 调试模式下命令处理
 * @param {char} *buf
 * @param {uint8_t} length
 * @return {*}
 */
void debug_process(char *buf,uint8_t length)
{
     if(strcmp((char*)buf,"setOutMode:1\n")==0)
    {
			printf("\r\n电容输出\r\n");
			SCB_Visual_Flag_t.Out_Mode = 1;
    }
    else if(strcmp((char*)buf,"setOutMode:0\n")==0)
    {
			printf("\r\n电池输出\r\n");
			SCB_Visual_Flag_t.Out_Mode = 0;
    }
    else if(strcmp((char*)buf,"setChargeMode:0\n")==0)
    {
			printf("\r\n电池输出\r\n");
			SCB_Visual_Flag_t.Charge_Mode = 0;
    }
    else if(strcmp((char*)buf,"setChargeMode:1\n")==0)
    {
			printf("\r\n开始充电\r\n");
			SCB_Visual_Flag_t.Charge_Mode = 1;
    }
    else if(strcmp((char*)buf,"wave_off")==0)
    {
        printf("\r\n波形发送关闭\r\n");
			SCB_Visual_Flag_t.Wave_Show = 0;
    }
    else if (strcmp((char*)buf,"wave_on")==0)
    {
        printf("\r\n波形发送启动\r\n");
			SCB_Visual_Flag_t.Wave_Show = 1;
    }
    else if (strcmp((char*)buf,"exit")==0)
    {
			printf("\r\nexit debug\r\n");
			SCB_Visual_Flag_t.Debug_Mode = 0;
    }
		else if (length>=20)
		{
			if(length==20)
				SCB_Visual_Flag_t.Charge_Power =(*(buf+18)-48);
			else
				SCB_Visual_Flag_t.Charge_Power =(*(buf+18)-48)*10+(*(buf+19)-48);
			
//			printf("%s",buf);
//			printf("%d",*(buf+19));
			printf("%d",SCB_Visual_Flag_t.Charge_Power);
		}
		else
		{
			printf("\ryou can input such as\n");
			  printf("\rwave_on\r:波形发送打开\n");
        printf("\rwave_off\r:波形发送关闭\r\n");
        printf("\rdebug\r:进入调试模式\n");
        printf("\rexit\r:退出调试模式\n");

		}
}
/**
 * @description: 处理Vofa上位机的数据，主要负责进入debug模式
 * @param {char} *buf
 * @param {uint8_t} length
 * @return {*}
 */
void Vofa_data_process(char *buf,uint8_t length)
{
    if(SCB_Visual_Flag_t.Debug_Mode == 1)
    {
        debug_process(buf,length);
    }
    else{
        if(strcmp((char*)buf,"debug")==0)
        {
            printf("\r\ndebug mode open\r\n");
						SCB_Visual_Flag_t.Debug_Mode = 1;
        }
        else
        {
            printf("\r\nplease use debug mode to debug!\r\n");
        }
    }
    if(strcmp((char*)buf,"help")==0)
    {
        printf("\rwave_on\r:波形发送打开\n");
        printf("\rwave_off\r:波形发送关闭\r\n");
        printf("\rdebug\r:进入调试模式\n");
        printf("\rexit\r:退出调试模式\n");

    }
 

   
			
}

/**
 * @description: Vofa上位机串口中断服务函数
 * @param {*}
 * @return {*}
 */
void vofa_shell_to_idle()
{
    if(USART2 == USRAT_PORT.Instance)                                   
    {	// 判断是否是空闲中断
        if(RESET != __HAL_UART_GET_FLAG(&USRAT_PORT, UART_FLAG_IDLE))   
        {	 // 清除空闲中断标志（否则会一直不断进入中断）
            __HAL_UART_CLEAR_IDLEFLAG(&USRAT_PORT);   
            // 停止本次DMA传输
            HAL_UART_DMAStop(&USRAT_PORT);                                
            // 计算接收到的数据长度
            uint32_t data_length  = RXBUF_SIZE - __HAL_DMA_GET_COUNTER(&USART_DMA_RX_PORT);
//					printf("Receive Data(length = %d): \n",data_length);
					// printf("\r\nUART3 Idle IQR Detected\r\n");
					  char pData[RXBUF_SIZE]={0};
						memset(pData,0,sizeof(pData));
						memcpy(pData,buffer,data_length);
            Vofa_data_process(pData,data_length);				
					
            // 重启开始DMA传输 每次255字节数据
            HAL_UART_Receive_DMA(&USRAT_PORT, (uint8_t*)buffer, RXBUF_SIZE);   
					
            // 调用中断处理函数
//            USAR_UART_IDLECallback(&huart1);                          
        }
    }
}
